﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YRevitJig;

namespace YArchitech.HVAC
{
	public class PELineJig : XUiJigBase, IDisposable
	{
		public PELineJig(UIDocument uiDoc) : base(uiDoc)
		{
			this.JigStage = PELineJig.EStage.eNoOper;
			this.ResetPoint = XUiJigBase.EResetPoint.eNoOper;
			this.DragLineColor = System.Drawing.Color.FromArgb(175, 222, 239);
			this.PointAlignType = PELineJig.AlignType.eMiddle;
			this.MutilLineDist = new List<double>();
			this.DrawEndLine = false;
			this.DrawSize = false;
			int height = Screen.PrimaryScreen.Bounds.Height;
			PELineJig.SizeOffset = (int)(0.006 * (double)height);
			PELineJig.BorderLength = (int)(0.04 * (double)height);
			PELineJig.SizeFont = new Font("Arial", 2.5f * (float)PELineJig.SizeOffset, GraphicsUnit.Pixel);
			PELineJig.TextFormat = new StringFormat(StringFormatFlags.NoClip);
			PELineJig.TextFormat.Alignment = StringAlignment.Center;
			PELineJig.TextFormat.LineAlignment = StringAlignment.Center;
			PELineJig.SizeLineColor = System.Drawing.Color.FromArgb(126, 163, 255);
			PELineJig.SizeTextBrush = new SolidBrush(base.JigPen.Color);
			this.OnMouseLeaveFormClientEvent = (WndMsgManager.MouseLeaveFormClientEvnetHandler)Delegate.Combine(this.OnMouseLeaveFormClientEvent, new WndMsgManager.MouseLeaveFormClientEvnetHandler(this.OnMouseLeaveFormClientEvnetHandler));
			this.OnMouseMoveIntoFormClientEvent = (WndMsgManager.MouseMoveIntoFormClientEvnetHandler)Delegate.Combine(this.OnMouseMoveIntoFormClientEvent, new WndMsgManager.MouseMoveIntoFormClientEvnetHandler(this.OnMouseMoveIntoFormClientEvnetHandler));
		}

		public PELineJig(UIDocument uiDoc, System.Windows.Forms.Form userForm) : base(uiDoc)
		{
			this.JigStage = PELineJig.EStage.eNoOper;
			this.ResetPoint = XUiJigBase.EResetPoint.eNoOper;
			this.DragLineColor = System.Drawing.Color.FromArgb(175, 222, 239);
			this.PointAlignType = PELineJig.AlignType.eMiddle;
			this.MutilLineDist = new List<double>();
			this.DrawEndLine = false;
			this.DrawSize = false;
			int height = Screen.PrimaryScreen.Bounds.Height;
			PELineJig.SizeOffset = (int)(0.006 * (double)height);
			PELineJig.BorderLength = (int)(0.04 * (double)height);
			PELineJig.SizeFont = new Font("Arial", 2.5f * (float)PELineJig.SizeOffset, GraphicsUnit.Pixel);
			PELineJig.TextFormat = new StringFormat(StringFormatFlags.NoClip);
			PELineJig.TextFormat.Alignment = StringAlignment.Center;
			PELineJig.TextFormat.LineAlignment = StringAlignment.Center;
			PELineJig.SizeLineColor = System.Drawing.Color.FromArgb(126, 163, 255);
			PELineJig.SizeTextBrush = new SolidBrush(base.JigPen.Color);
			this.OnMouseLeaveFormClientEvent = (WndMsgManager.MouseLeaveFormClientEvnetHandler)Delegate.Combine(this.OnMouseLeaveFormClientEvent, new WndMsgManager.MouseLeaveFormClientEvnetHandler(this.OnMouseLeaveFormClientEvnetHandler));
			this.OnMouseMoveIntoFormClientEvent = (WndMsgManager.MouseMoveIntoFormClientEvnetHandler)Delegate.Combine(this.OnMouseMoveIntoFormClientEvent, new WndMsgManager.MouseMoveIntoFormClientEvnetHandler(this.OnMouseMoveIntoFormClientEvnetHandler));
			this.m_UserForm = userForm;
		}

		public new void Dispose()
		{
			base.Dispose();
			PELineJig.SizeFont.Dispose();
			PELineJig.TextFormat.Dispose();
			PELineJig.SizeTextBrush.Dispose();
			this.OnMouseLeaveFormClientEvent = (WndMsgManager.MouseLeaveFormClientEvnetHandler)Delegate.Remove(this.OnMouseLeaveFormClientEvent, new WndMsgManager.MouseLeaveFormClientEvnetHandler(this.OnMouseLeaveFormClientEvnetHandler));
			this.OnMouseMoveIntoFormClientEvent = (WndMsgManager.MouseMoveIntoFormClientEvnetHandler)Delegate.Remove(this.OnMouseMoveIntoFormClientEvent, new WndMsgManager.MouseMoveIntoFormClientEvnetHandler(this.OnMouseMoveIntoFormClientEvnetHandler));
		}

		private void OnMouseLeaveFormClientEvnetHandler(object sender, EventArgs e)
		{
			System.Windows.Forms.Form userForm = this.m_UserForm;
		}

		private void OnMouseMoveIntoFormClientEvnetHandler(object sender, EventArgs e)
		{
			System.Windows.Forms.Form userForm = this.m_UserForm;
		}

		public XYZ PickStartPoint(string strPrompt = "请确定直线起点：")
		{
			try
			{
				this.JigStage = PELineJig.EStage.eStartPt;
				this.StartPoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				this.StartPoint = null;
			}
			this.JigStage = PELineJig.EStage.eNoOper;
			return this.StartPoint;
		}

		public XYZ PickEndPoint(XYZ ptStart, string strPrompt = "请确定直线终点：")
		{
			try
			{
				this.JigStage = PELineJig.EStage.eEndPt;
				this.StartPoint = ptStart;
				this.EndPoint = base.UiDoc.Selection.PickPoint(strPrompt);
				this.EndPoint = this.GetResetPoint(this.StartPoint, this.EndPoint);
			}
			catch
			{
				if (this.m_prompDlg != null && base.CmdEndType == XUiJigBase.ECmdEndType.eEnter)
				{
					base.CmdEndType = XUiJigBase.ECmdEndType.eOther;
					XYZ xyz = base.Client2Revit(this.m_ptCur);
					xyz = Line.CreateUnbound(xyz, base.UiDoc.ActiveView.ViewDirection).Project(this.StartPoint).XYZPoint;
					xyz = this.GetResetPoint(this.StartPoint, xyz);
					double num = NumbericTools.mmToFeet(Convert.ToDouble(this.m_prompDlg.GetInputValue()));
					XYZ xyz2 = (xyz - this.StartPoint).Normalize();
					this.EndPoint = this.StartPoint + xyz2 * num;
				}
				else
				{
					this.EndPoint = null;
				}
			}
			this.JigStage = PELineJig.EStage.eNoOper;
			return this.EndPoint;
		}

		public XYZ GetResetPoint(XYZ ptBase, XYZ ptUser)
		{
			XYZ result = ptUser;
			if (this.ResetPoint == XUiJigBase.EResetPoint.eByAngle)
			{
				result = JigFuncs.GetProjectPoint(base.UiDoc, ptUser, ptBase, this.LineAngle);
			}
			else if (this.ResetPoint == XUiJigBase.EResetPoint.eByOrtho)
			{
				result = JigFuncs.GetOrthoPoint(base.UiDoc, ptUser, ptBase);
			}
			else if (this.ResetPoint == XUiJigBase.EResetPoint.eByAxis)
			{
				XYZ axisPoint = JigFuncs.GetAxisPoint(base.UiDoc, ptUser, ptBase);
				if (axisPoint != null)
				{
					result = axisPoint;
				}
			}
			return result;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			if (this.m_ptCur.IsEmpty || this.JigStage != PELineJig.EStage.eEndPt)
			{
				return;
			}
			XYZ xyz = base.Client2Revit(this.m_ptCur);
			if (this.ResetPoint == XUiJigBase.EResetPoint.eByAngle)
			{
				xyz = JigFuncs.GetProjectPoint(base.UiDoc, xyz, this.StartPoint, this.LineAngle);
				this.m_ptCur = base.Revit2Client(xyz);
			}
			else if (this.ResetPoint == XUiJigBase.EResetPoint.eByOrtho)
			{
				xyz = JigFuncs.GetOrthoPoint(base.UiDoc, xyz, this.StartPoint);
				this.m_ptCur = base.Revit2Client(xyz);
			}
			else if (this.ResetPoint == XUiJigBase.EResetPoint.eByAxis)
			{
				XYZ axisPoint = JigFuncs.GetAxisPoint(base.UiDoc, xyz, this.StartPoint);
				if (axisPoint != null)
				{
					xyz = axisPoint;
					this.m_ptCur = base.Revit2Client(xyz);
					base.DrawAxisLine(paintEventArgs.Graphics, this.StartPoint, xyz);
				}
			}
			XGraphics xgraphics = new XGraphics(paintEventArgs.Graphics, base.JigPen);
			System.Drawing.Color color = xgraphics.m_pen.Color;
			xgraphics.m_pen.Color = this.DragLineColor;
			if (this.MutilLineDist.Count < 1)
			{
				new XLine2D(base.Revit2Client(this.StartPoint), this.m_ptCur).Draw(xgraphics);
				if (this.DrawSize)
				{
					double feet = this.StartPoint.DistanceTo(xyz);
					string strSizeText = string.Format("{0:F1}", NumbericTools.feetTomm(feet));
					PELineJig.DrawLineSize(base.Revit2Client(this.StartPoint), base.Revit2Client(xyz), strSizeText, xgraphics);
				}
			}
			else
			{
				this.DrawMutilLine(this.StartPoint, xyz, xgraphics);
			}
			xgraphics.m_pen.Color = color;
		}

		public override List<Curve> CreateCurves()
		{
			List<Curve> list = new List<Curve>();
			try
			{
				if (this.StartPoint != null && this.EndPoint != null)
				{
					if (this.MutilLineDist.Count < 1)
					{
						list.Add(Line.CreateBound(this.StartPoint, this.EndPoint));
					}
					else
					{
						XYZ xyz = this.StartPoint;
						XYZ xyz2 = this.EndPoint;
						XYZ xyz3 = (xyz2 - xyz).Normalize();
						XYZ xyz4 = Transform.CreateRotation(base.UiDoc.ActiveView.ViewDirection, -Math.PI*.5).OfVector(xyz3);
						double num = 0.0;
						foreach (double num2 in this.MutilLineDist)
						{
							num += num2;
						}
						double num3 = 0.0;
						if (this.PointAlignType == PELineJig.AlignType.eRight)
						{
							num3 = num;
						}
						else if (this.PointAlignType == PELineJig.AlignType.eMiddle)
						{
							num3 = num / 2.0;
						}
						xyz -= xyz4 * num3;
						xyz2 -= xyz4 * num3;
						if (this.DrawEndLine)
						{
							XYZ xyz5 = xyz + xyz4 * num;
							XYZ xyz6 = xyz2 + xyz4 * num;
							list.Add(Line.CreateBound(xyz, xyz5));
							list.Add(Line.CreateBound(xyz2, xyz6));
						}
						list.Add(Line.CreateBound(xyz, xyz2));
						foreach (double num4 in this.MutilLineDist)
						{
							xyz += xyz4 * num4;
							xyz2 += xyz4 * num4;
							list.Add(Line.CreateBound(xyz, xyz2));
						}
					}
				}
			}
			catch
			{
			}
			return list;
		}

		public double LineAngle { get; set; }

		public XYZ StartPoint { get; set; }

		public XYZ EndPoint { get; set; }

		public PELineJig.EStage JigStage { get; set; }

		public XUiJigBase.EResetPoint ResetPoint { get; set; }

		public System.Drawing.Color DragLineColor { get; set; }

		public PELineJig.AlignType PointAlignType { get; set; }

		public List<double> MutilLineDist { get; set; }

		public void DrawMutilLine(XYZ ptBaseS, XYZ ptBaseE, XGraphics graphics)
		{
			Transform transform = Transform.CreateRotation(base.UiDoc.ActiveView.ViewDirection, -Math.PI*.5);
			XYZ xyz = (ptBaseE - ptBaseS).Normalize();
			XYZ xyz2 = transform.OfVector(xyz);
			double num = 0.0;
			foreach (double num2 in this.MutilLineDist)
			{
				num += num2;
			}
			double num3 = 0.0;
			if (this.PointAlignType == PELineJig.AlignType.eRight)
			{
				num3 = num;
			}
			else if (this.PointAlignType == PELineJig.AlignType.eMiddle)
			{
				num3 = num / 2.0;
			}
			ptBaseS -= xyz2 * num3;
			ptBaseE -= xyz2 * num3;
			if (this.DrawEndLine)
			{
				XYZ point = ptBaseS + xyz2 * num;
				XYZ point2 = ptBaseE + xyz2 * num;
				XLine2D xline2D = new XLine2D(base.Revit2Client(ptBaseS), base.Revit2Client(point));
				XDrawable2D xdrawable2D = new XLine2D(base.Revit2Client(ptBaseE), base.Revit2Client(point2));
				xline2D.Draw(graphics);
				xdrawable2D.Draw(graphics);
			}
			new XLine2D(base.Revit2Client(ptBaseS), base.Revit2Client(ptBaseE)).Draw(graphics);
			foreach (double num4 in this.MutilLineDist)
			{
				ptBaseS += xyz2 * num4;
				ptBaseE += xyz2 * num4;
				new XLine2D(base.Revit2Client(ptBaseS), base.Revit2Client(ptBaseE)).Draw(graphics);
			}
		}

		public bool DrawEndLine { get; set; }

		public bool DrawSize { get; set; }

		public static int SizeOffset { get; set; }

		public static int BorderLength { get; set; }

		public static Font SizeFont { get; set; }

		public static StringFormat TextFormat { get; set; }

		public static SolidBrush SizeTextBrush { get; set; }

		public static System.Drawing.Color SizeLineColor { get; set; }

		public static void DrawLineSize(System.Drawing.Point ptStart, System.Drawing.Point ptEnd, string strSizeText, XGraphics graphics)
		{
			XUiJigBase.SortTwoPoint(ref ptStart, ref ptEnd);
			if (XUiJigBase.TwoPointDist(ptStart, ptEnd) < 10)
			{
				return;
			}
			double num = XUiJigBase.TwoPointAngle(ptStart, ptEnd);
			double dAngle = num + Math.PI*.5;
			System.Drawing.Color color = graphics.m_pen.Color;
			graphics.m_pen.Color = PELineJig.SizeLineColor;
			System.Drawing.Point ptFrom = XUiJigBase.OffsetPoint(ptStart, dAngle, PELineJig.SizeOffset);
			System.Drawing.Point ptFrom2 = XUiJigBase.OffsetPoint(ptEnd, dAngle, PELineJig.SizeOffset);
			System.Drawing.Point ptFrom3 = XUiJigBase.OffsetPoint(ptFrom, dAngle, PELineJig.BorderLength - PELineJig.SizeOffset);
			System.Drawing.Point ptFrom4 = XUiJigBase.OffsetPoint(ptFrom2, dAngle, PELineJig.BorderLength - PELineJig.SizeOffset);
			new XLine2D(XUiJigBase.OffsetPoint(ptFrom3, num, -PELineJig.SizeOffset), XUiJigBase.OffsetPoint(ptFrom4, num, PELineJig.SizeOffset)).Draw(graphics);
			new XLine2D(XUiJigBase.OffsetPoint(ptFrom3, num + 45.0, PELineJig.SizeOffset), XUiJigBase.OffsetPoint(ptFrom3, num + 45.0, -PELineJig.SizeOffset)).Draw(graphics);
			new XLine2D(XUiJigBase.OffsetPoint(ptFrom4, num + 45.0, PELineJig.SizeOffset), XUiJigBase.OffsetPoint(ptFrom4, num + 45.0, -PELineJig.SizeOffset)).Draw(graphics);
			System.Drawing.Point point = XUiJigBase.OffsetPoint(XUiJigBase.OffsetPoint(ptFrom, num, XUiJigBase.TwoPointDist(ptStart, ptEnd) / 2), dAngle, (int)((double)PELineJig.BorderLength + (double)PELineJig.SizeFont.Height / 2.0));
			PointF point2 = new PointF((float)point.X, (float)point.Y);
			float angle = -(float)(num / Math.PI * 180.0);
			graphics.m_graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
			Matrix transform = graphics.m_graphics.Transform;
			Matrix transform2 = graphics.m_graphics.Transform;
			transform2.RotateAt(angle, point2);
			graphics.m_graphics.Transform = transform2;
			graphics.m_graphics.DrawString(strSizeText, PELineJig.SizeFont, PELineJig.SizeTextBrush, point2, PELineJig.TextFormat);
			graphics.m_graphics.Transform = transform;
			graphics.m_pen.Color = color;
		}

		public static void DrawLinesSize(System.Drawing.Point ptStart, System.Drawing.Point ptEnd, string strSizeText, XGraphics graphics)
		{
			XUiJigBase.SortTwoPoint(ref ptStart, ref ptEnd);
			if (XUiJigBase.TwoPointDist(ptStart, ptEnd) < 10)
			{
				return;
			}
			double num = XUiJigBase.TwoPointAngle(ptStart, ptEnd);
			double dAngle = num + Math.PI*.5;
			System.Drawing.Color color = graphics.m_pen.Color;
			graphics.m_pen.Color = PELineJig.SizeLineColor;
			System.Drawing.Point point = XUiJigBase.OffsetPoint(ptStart, dAngle, PELineJig.SizeOffset);
			System.Drawing.Point point2 = XUiJigBase.OffsetPoint(ptEnd, dAngle, PELineJig.SizeOffset);
			System.Drawing.Point ptEnd2 = XUiJigBase.OffsetPoint(point, dAngle, PELineJig.BorderLength);
			new XLine2D(point, ptEnd2).Draw(graphics);
			System.Drawing.Point ptEnd3 = XUiJigBase.OffsetPoint(point2, dAngle, PELineJig.BorderLength);
			new XLine2D(point2, ptEnd3).Draw(graphics);
			System.Drawing.Point ptFrom = XUiJigBase.OffsetPoint(point, dAngle, PELineJig.BorderLength - PELineJig.SizeOffset);
			System.Drawing.Point ptFrom2 = XUiJigBase.OffsetPoint(point2, dAngle, PELineJig.BorderLength - PELineJig.SizeOffset);
			new XLine2D(XUiJigBase.OffsetPoint(ptFrom, num, -PELineJig.SizeOffset), XUiJigBase.OffsetPoint(ptFrom2, num, PELineJig.SizeOffset)).Draw(graphics);
			new XLine2D(XUiJigBase.OffsetPoint(ptFrom, num + 45.0, PELineJig.SizeOffset), XUiJigBase.OffsetPoint(ptFrom, num + 45.0, -PELineJig.SizeOffset)).Draw(graphics);
			new XLine2D(XUiJigBase.OffsetPoint(ptFrom2, num + 45.0, PELineJig.SizeOffset), XUiJigBase.OffsetPoint(ptFrom2, num + 45.0, -PELineJig.SizeOffset)).Draw(graphics);
			System.Drawing.Point point3 = XUiJigBase.OffsetPoint(XUiJigBase.OffsetPoint(point, num, XUiJigBase.TwoPointDist(ptStart, ptEnd) / 2), dAngle, (int)((double)PELineJig.BorderLength + (double)PELineJig.SizeFont.Height / 2.0));
			PointF point4 = new PointF((float)point3.X, (float)point3.Y);
			float angle = -(float)(num / Math.PI * 180.0);
			graphics.m_graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
			Matrix transform = graphics.m_graphics.Transform;
			Matrix transform2 = graphics.m_graphics.Transform;
			transform2.RotateAt(angle, point4);
			graphics.m_graphics.Transform = transform2;
			graphics.m_graphics.DrawString(strSizeText, PELineJig.SizeFont, PELineJig.SizeTextBrush, point4, PELineJig.TextFormat);
			graphics.m_graphics.Transform = transform;
			graphics.m_pen.Color = color;
		}

		public enum EStage
		{
			eNoOper,
			eStartPt,
			eEndPt
		}

		public enum AlignType
		{
			eMiddle,
			eLeft,
			eRight
		}
	}
}
